/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.daydream.template.sequence.annotation;

import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;

import org.pajamas.daydream.template.sequence.Sequence;
import org.pajamas.daydream.template.sequence.SequenceContext;
import org.pajamas.daydream.template.sequence.SequenceTemplate;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import lombok.Getter;
import lombok.Setter;

/**
 * @author william
 * @since 2024/4/13
 */
@Getter
public final class SequenceBinding {
    @Nonnull
    private final Sequence sequence;
    private final List<SequenceContext> runtimeContexts;
    @Setter
    private String sequenceString;

    private SequenceBinding(@Nonnull Sequence sequence, List<SequenceContext> runtimeContexts) {
        this.sequence = Objects.requireNonNull(sequence);
        this.runtimeContexts = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(runtimeContexts)) {
            this.runtimeContexts.addAll(runtimeContexts
                    .stream()
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList()));
        }
    }

    public static SequenceBinding.Builder newBuilder() {
        return new Builder();
    }

    public void addFirstContext(SequenceContext context) {
        if (context != null) {
            this.runtimeContexts.add(0, context);
        }
    }

    public static class Builder implements org.pajamas.protocol.Builder<SequenceBinding> {
        private final List<SequenceContext> runtimeContexts;
        private Sequence sequence;

        private Builder() {
            this.runtimeContexts = Lists.newArrayList();
        }

        public Builder sequence(Sequence sequence) {
            this.sequence = sequence;
            return this;
        }

        public Builder createSequence(SequenceTemplate template, String pattern) {
            this.sequence = MoreObjects.firstNonNull(template, SequenceTemplate.CONTEXT_LESS).create(pattern);
            return this;
        }

        public Builder createSequence(String pattern) {
            return this.createSequence(null, pattern);
        }

        public Builder runtimeContexts(List<SequenceContext> runtimeContexts) {
            if (!CollectionUtils.isEmpty(runtimeContexts)) {
                this.runtimeContexts.addAll(runtimeContexts);
            }
            return this;
        }

        public Builder addRuntimeContext(SequenceContext sequenceContext) {
            this.runtimeContexts.add(sequenceContext);
            return this;
        }

        @Override
        public SequenceBinding build() {
            return new SequenceBinding(this.sequence, this.runtimeContexts);
        }
    }
}
